package com.eascs.o2o.loan.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.service.ILoanInfoService;
import com.eascs.admin.common.service.IOverdueService;
import com.eascs.admin.common.vo.ModelResult;
import com.eascs.admin.common.vo.OverdueEntity;
import com.eascs.admin.common.vo.OverdueReportPO;
import com.eascs.admin.common.vo.WeChatParams;
import com.eascs.crm.common.service.ICustomerInfoService;
import com.eascs.o2o.common.controller.BaseMasterViewController;
import com.eascs.o2o.loan.form.LoanLoginForm;
import com.eascs.site.view.ViewContext;

@Controller
@RequestMapping("/loan")
public class LoanController extends BaseMasterViewController {

	@Reference
	private IOverdueService overdueService;

	@Reference
	private ICustomerInfoService customerInfoService;

	private final static String ID_TYPE = "IDC";

	@Reference
	ILoanInfoService iLoanInfoService;

	@RequestMapping("/login")
	public String login() {
		return "/loan/login";
	}

	@RequestMapping("/submit/login")
	public String subLogin(LoanLoginForm loanLoginForm) {
		if (loanLoginForm == null) {
			ViewContext.put("errorMsg", "参数不能为空");
			return "/loan/login";
		}
		if (StringUtils.isBlank(loanLoginForm.getUname())) {
			ViewContext.put("errorMsg", "用户名不能为空");
			return "/loan/login";
		}
		if (StringUtils.isBlank(loanLoginForm.getIdCard())) {
			ViewContext.put("errorMsg", "身份证不能为空");
			return "/loan/login";
		}
		if (StringUtils.isBlank(loanLoginForm.getMobile())) {
			ViewContext.put("errorMsg", "电话号码不能为空");
			return "/loan/login";
		}
		ModelResult<OverdueEntity> result = overdueService.queryHKZ(loanLoginForm.getIdCard(), ID_TYPE,
				loanLoginForm.getUname(), loanLoginForm.getMobile());
		String errorMsg = null;
		if (result.isSuccess()) {
			OverdueEntity overdue = result.getModel();
			OverdueReportPO po = overdue.getOverdueReportPO();
			if (po != null && po.isHasData()) {
				ViewContext.put("report", overdue.getOverdueReportPO());
				ViewContext.put("refunds", overdue.getRefundLineList());
				return "/loan/loanDetails";
			}
			errorMsg = "未查询到该客户";
		}
		if (StringUtils.isBlank(errorMsg)) {
			errorMsg = "查询异常，请联系管理员";
		}
		ViewContext.put("errorMsg", errorMsg);
		return "/loan/login";
	}

	@RequestMapping("/scheduelInquery")// 进入贷款进度查询页面，只有登录的用户才能使用该功能
	public String scheduelInquery(HttpServletRequest request,HttpServletResponse response)
	{
		String uname="";
		String idCard="";
		String moblie="";
		try {
			Cookie[] cookies = request.getCookies();
	        if (cookies!=null) {
	        	for (Cookie cookie : cookies) {
	        		String key=cookie.getName();
	        		String value=cookie.getValue();
	        		if(StringUtils.equals(key, "uname"))
	        			uname= URLDecoder.decode(value, "UTF-8");
	        		if(StringUtils.equals(key, "idCard"))
	        			idCard= value;
	        		if(StringUtils.equals(key, "moblie"))
	        			moblie= value;
				}
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
        ViewContext.put("uname", uname);
        ViewContext.put("idCard", idCard);
        ViewContext.put("moblie", moblie);
		return "loan/scheduelInquery";
	}
     
	@RequestMapping("/scheduelInqueryResult")//进入查询进度查询结果页面
	 public String InqueryResult(HttpServletRequest request,HttpServletResponse response,LoanLoginForm form){
		String scheduleState;      //存放提示信息
		WeChatParams params =new WeChatParams();
		params.setName(form.getUname());
		params.setPhone(form.getMobile());
		params.setIdCard(form.getIdCard());
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		try {
			Cookie uname = new Cookie("uname", URLEncoder.encode(form.getUname(), "UTF-8"));
			uname.setMaxAge(24*60*60*10);
			response.addCookie(uname);
			
			Cookie idCard = new Cookie("idCard", URLEncoder.encode(form.getIdCard(), "UTF-8"));
			uname.setMaxAge(24*60*60*10);
			response.addCookie(idCard);
			
			Cookie moblie = new Cookie("moblie", URLEncoder.encode(form.getMobile(), "UTF-8"));
			moblie.setMaxAge(24*60*60*10);
			response.addCookie(moblie);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		ModelResult<String> model = iLoanInfoService.getLoanApplyStatus(params);//根据参数查询进度
	    if(model.getErrMsg()==null)//若无异常信息，就取得贷款进度返回给用户 
	    {
	    	 scheduleState=model.getModel();
	    	 ViewContext.put("scheduleState", scheduleState);
	    	 return"loan/scheduleResult";
	    }
	    if(model.getModel()==null){ //若有进度有异常，便返回异常信息给用户
	    	 scheduleState=model.getErrMsg();
	    	 ViewContext.put("scheduleState", scheduleState);
	    	 return"loan/scheduleError";
	    }
		
		if (model.getModel() != null) {
			if(model.getModel().equals("作废")||model.getModel().equals("新建")||model.getModel().equals("已放款")||model.getModel().equals("还款完成")
					||model.getModel().equals("银行已拒绝")	){
				scheduleState = "您未有审批中的贷款申请，您的贷款申请可能已放款或未通过";
				ViewContext.put("scheduleState", scheduleState);
				return "loan/scheduleError";
			}
			else{
				
				scheduleState = "征信审查中";
				ViewContext.put("scheduleState", scheduleState);
				return "loan/scheduleResult";
			}
			
		}
        return null;
	}

}
